/**
 * 作者: wgx
 * 日期: 2025-05-30 15:45:18
 * 备注: 用户技能加成数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserSkillEffect;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserSkillEffectModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id.effect_type";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected long userId; // 用户Id
    protected int effectType; // 加成类型
    protected String value = ""; // 加成值 (jsonarray格式的字符串，index0表示全部)

    public void reset() {
    }

    protected String getKey() {
        return "UserSkillEffect." + userId + "." + effectType;
    }

    protected String getListKey() {
        return "UserSkillEffect." + userId;
    }

    protected String getListItemKey() {
        return effectType + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_skill_effect"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("userId", userId); // 用户Id
        obj.put("effectType", effectType); // 加成类型
        obj.put("value", value); // 加成值 (jsonarray格式的字符串，index0表示全部)
        return obj;
    }

    public PBUserSkillEffect.Builder takeInitBuilder() {
        PBUserSkillEffect.Builder sb = PBUserSkillEffect.newBuilder();
        if (effectType != 0) {
            sb.setEffectType(effectType);
        }
        if (this.value != null && this.value.length() > 0) {
            JsonArray list = new JsonArray(this.value);
            for(int i=0; i<list.size(); i++) {
                sb.addValue(list.getInteger(i));
            }
        }
        return sb;
    }

    public PBUserSkillEffect.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserSkillEffect.Builder sb = PBUserSkillEffect.newBuilder();
        if (isFieldUpdate("effectType")) {
            sb.setEffectType(effectType);
        }
        if (isFieldUpdate("value")) {
            if (this.value != null && this.value.length() > 0) {
                JsonArray list = new JsonArray(this.value);
                for(int i=0; i<list.size(); i++) {
                    sb.addValue(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearValue();
                }
            } else {
                sb.clearValue();
            }
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getEffectType() {
        return effectType;
    }

    public void putEffectType(int effectType) {
        if (this.effectType != effectType) {
            this.effectType = effectType;
            updateRedisHashField(getKey(), "effectType", this.effectType);
        }
    }

    public void addEffectType(int value) {
        this.effectType += value;
        updateRedisHashField(getKey(), "effectType", this.effectType);
    }

    public JsonArray getValueArray() {
        if (this.value != null && this.value.length() > 0) {
            return new JsonArray(this.value);
        } else {
            return new JsonArray();
        }
    }

    public String getValue() {
        return value;
    }

    public void putValue(String value) {
        if (!this.value.equals(value)) {
            this.value = value;
            updateRedisHashField(getKey(), "value", this.value);
        }
    }

}